package com.laivi.basic.model.file.export;

import java.util.List;

import com.laivi.basic.model.datastruct.ValueMap;
import com.laivi.basic.model.file.writer.ExcelWriter;
import com.laivi.basic.util.DataUtil;

public class ExcelExport implements IExport{
	private String path;
	private List<ValueMap> datas;
	private ExcelWriter writer;
	
	public ExcelExport(){
	}
	
	public ExcelExport(String path, List<ValueMap> datas) {
		super();
		this.path = path;
		this.datas = datas;
	}

	@Override
	public void export() {
		writer=new ExcelWriter(path);
		writer.setSheet(0);
		String[] fields=null;
		if(datas!=null && datas.size()>0){
			fields=datas.get(0).getKeys();      //save fields
			//write head
			int i=0;
			for(String field:fields){
				writer.writeCell(0, i,field);
				i++;
			}
			//write data
			int row=1;
			for(ValueMap data:datas){
				i=0;
				for(String field:fields){
					writer.writeCell(row, i, DataUtil.emptyBack(data.get(field)));
					i++;
				}
				row++;
			}
		}
		writer.close();
	}

	@Override
	public void export(String[] titles,String[] fields) {
		writer=new ExcelWriter(path);
		writer.setSheet(0);
		if(datas!=null && datas.size()>0){
			//write head
			int i=0;
			for(String title:titles){
				writer.writeCell(0, i,title);
				i++;
			}
			//write data
			int row=1;
			for(ValueMap data:datas){
				i=0;
				for(String field:fields){
					writer.writeCell(row, i, DataUtil.emptyBack(data.get(field)));
					i++;
				}
				row++;
			}
		}
		writer.close();
	}
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public List<ValueMap> getDatas() {
		return datas;
	}

	public void setDatas(List<ValueMap> datas) {
		this.datas = datas;
	}
}
